
package com.v1.video.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.v1.video.R;
import com.v1.video.domain.LoginInfo;
import com.v1.video.util.Utils;

public class PersonSignActivity extends BaseActivity {

    private final String TAG = "PersonSignActivity";

    private PersonSignActivity instance;
//    setText(Html.fromHtml("<u>"+"hahaha"+"</u>"));
    
    /**标题*/
    TextView text_title;
    /**返回按钮*/
    private ImageView view_btn_return;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_sign);
        instance = this;
    }

    @Override
    protected void initView() {
        // TODO Auto-generated method stub
        
        view_btn_return = (ImageView)findViewById(R.id.iv_result);
        text_title = (TextView)findViewById(R.id.tv_title);
        editText = (EditText)findViewById(R.id.person_sign_edit_input);
        textRemainNum = (TextView)findViewById(R.id.person_sign_text_number);
        
    }

    @Override
    protected void initData() {
        // TODO Auto-generated method stub
        text_title.setText(R.string.person_info_personality_sign);
        
//        initEdit();
        editText.addTextChangedListener(watcher);
        editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(length)});        
        if(LoginInfo.getInstance().getDetail() != null && !LoginInfo.getInstance().getDetail().equals("")){
        	editText.setText(LoginInfo.getInstance().getDetail());
        }
        int strLength = editText.getText().toString().length();
//        textRemainNum.setText("" + (length - strLength <=0?0:length - strLength));
        textRemainNum.setText(strLength + "/" + "30");
//        initEdit();
    }

    @Override
    protected void setListener() {
        // TODO Auto-generated method stub
       findViewById(R.id.lay_result).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                finish();
            }
        });
        findViewById(R.id.person_sign_btn_save).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(editText.getText().toString().length() > length){
					Utils.showToast(instance, "字数过多", Toast.LENGTH_SHORT);
				}
				else{
					Intent intent = new Intent(instance, PersonInfoActivity.class);
					intent.putExtra("sign", editText.getText().toString());
					setResult(205, intent);
					finish();
				}
			}
		});
    }

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
        finish();
    }

    //字数限制相关
    
    private EditText editText;
    private TextView textRemainNum;
    private final int length = 30;
//    private void initEdit(){
//    	
//    	InputFilter[] filters = new InputFilter[1];
//        filters[0] = new InputFilter.LengthFilter(length) {
//            @Override
//            public CharSequence filter(CharSequence source, int start, int end,
//                    Spanned dest, int dstart, int dend) {                
//            	int strLength = editText.getText().toString().length();
//            	Logger.i(TAG, "内容="+editText.getText().toString()+"strLength=="+strLength);
//            	textRemainNum.setText("" + (length - strLength <=0?0:length - strLength));
//            	return super.filter(source, start, end, dest, dstart, dend);
//            }
//        };
//        editText.setFilters(filters);
//        int strLength = editText.getText().toString().length();
//        textRemainNum.setText("" + (length - strLength <=0?0:length - strLength));
//    }
    private TextWatcher watcher = new TextWatcher(){

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub
            
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            int strLength = editText.getText().toString().length();
//            textRemainNum.setText("" + (length - strLength <=0?0:length - strLength));
            textRemainNum.setText(strLength + "/" + "30");
        }        
    };
}
